package cate.game.attr;


import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.table.item.yoke.row.PowerAttrRow;
import cate.common.table.item.yoke.row.StarAttrRow;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.YokeOn;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;

import java.beans.Transient;
import java.util.List;

public class YokePowerAttrPart extends PowerAttrPart {
    public YokePowerAttrPart() {
    }

    public YokePowerAttrPart(PlayBuildContext ctx, YokeOn po) {
        if (po == null) {
            return;
        }
        Hero hero = po.owner;
        if (hero.getRole() == null) {
            return;
        }
        HeroBaseRow tpl = hero.getTpl(ctx);
        if (tpl == null) {
            return;
        }
        FightAttr attr = new FightAttr();
        if (po.heroUidMap != null) {
            for (Byte pos : po.heroUidMap.keySet()) {
                Hero h = po.getPosItem(pos);
                if (h == null) {
                    continue;
                }
                HeroBaseRow row = h.getTpl(ctx);
                if (row == null) {
                    continue;
                }
                StarAttrRow starAttrRow = getStarAttrRow(ctx, tpl.protoId, row.star);
                if (starAttrRow != null ) {
                        String starAttr = getStarAttr(starAttrRow, pos);
                        if (StringUtils.isNotBlank(starAttr)) {
                            FightAttr sAttr = new FightAttr(starAttr);
                            attr.add(sAttr);
                    }
                }

                List<PowerAttrRow> powerAttrRows = getPowerAttrRow(ctx, tpl.protoId, h.getPower(ctx));
                if (powerAttrRows != null && !powerAttrRows.isEmpty()) {
                    for (PowerAttrRow powerAttrRow : powerAttrRows) {
                        String powerAttr = getPowerAttr(powerAttrRow, pos);
                        if (StringUtils.isNotBlank(powerAttr)) {
                            FightAttr fAttr = new FightAttr(powerAttr);
                            attr.add(fAttr);
                        }
                    }
                }


            }
        }
        this.attr = attr;
        this.powerExt = 0;
    }

    @JsonIgnore
    @Transient
    private StarAttrRow getStarAttrRow(PlayBuildContext ctx, int protoId, int star) {
        return ctx.yokeExcel().starAttr.getRowByProtoIdAndStar(protoId, star);
    }

    @JsonIgnore
    @Transient
    private List<PowerAttrRow> getPowerAttrRow(PlayBuildContext ctx, int protoId, long power) {
        return ctx.yokeExcel().powerAttr.getRowByProtoIdAndPower(protoId, power);
    }

    @JsonIgnore
    @Transient
    private String getStarAttr(StarAttrRow row, int pos) {
        switch (pos) {
            case 1:
                return row.attr1;
            case 2:
                return row.attr2;
            case 3:
                return row.attr3;
            case 4:
                return row.attr4;
            default:
                return null;
        }
    }

    @JsonIgnore
    @Transient
    private String getPowerAttr(PowerAttrRow row, int pos) {
        switch (pos) {
            case 1:
                return row.attr1;
            case 2:
                return row.attr2;
            case 3:
                return row.attr3;
            case 4:
                return row.attr4;
            default:
                return null;
        }
    }

}
